<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>hello world</title>
		<script type="text/javascript" src="js/mui.min.js"></script>
	</head>

	<body>

		<script src="js/localData.js"></script>
		<script type="text/javascript" src="js/md5.js"></script>
		<script type="text/javascript" src="js/request.js"></script>
		<script type="text/javascript">
			if (window.plus) {
				plusready();
			} else {
				document.addEventListener('plusready', plusready, false);
			}

			function plusready() {
				//改变token
				changeToken();
				//获取位置信息
				getUserAddress();
			}
			/*定位方式获取用户地理位置
			 * geoInf定位公共回调函数
			 * function(e)失败函数
			 */
			function getUserAddress() {
				plus.geolocation.getCurrentPosition(geoInf, function(e) {
					//发生错误默认城市，经度纬度，
					alert(JSON.stringify(e));
					localStorage.currentCity = writeData("北京市");
					localStorage.currentLon = writeData("116.4");
					localStorage.currentLat = writeData("39.9");
					redirect();
				}, {
					geocode: true,
					provider: 'baidu'
				});
			}
			//定位成功
			//position定位返回成功参数json对象
			function geoInf(position) {
				console.log(JSON.stringify(position))
				localStorage.currentCity = writeData(position.address.city);
				localStorage.currentLon = writeData(position.coords.longitude);
				localStorage.currentLat = writeData(position.coords.latitude);
				redirect();
			}
			//设置跳转的方向
			function redirect() {
				if (localStorage.App == "false") {
					mui.openWindow({
						url: "html/homeMain.html",
						id: "homeMain.html",
						waiting: {
							autoShow: false, //自动显示等待框，默认为true
						}
					})
				} else {
					localStorage.App = "false";
					mui.openWindow({
						url: "html/guide.html",
						id: "guide.html",
						waiting: {
							autoShow: false, //自动显示等待框，默认为true
						}
					})
				}
			}
			//更换token
			function changeToken() {
				if (localStorage.account) {
					var accountOld = readData(localStorage.account);
					if (accountOld.length > 0) {
						tokens = readData(localStorage.token);
						/*
						//获取上次token有效期
						var validDate = new Date(parseInt(localStorage.tokenExpire))
						//获取当前时间
						var nowDate = new Date()
						//如果token超出有效期，进行新token更换
						if(nowDate>validDate){ //token更新 };
						*/
						//打开应时，如果无需登录，无论token是否过期，直接更新一个新的Token，防止用户在使用过程中token过期
						urlToken = getUrl('59token');
						headerToken = getHeader();
						mui.ajax(urlToken, {
							data: {
								id: accountOld,
								token: tokens
							},
							dataType: 'json',
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							headers: headerToken,
							success: function(ret) {
								if (ret) {
									if (ret.code == '0') {
										localStorage.token = writeData(ret.obj.token);
										localStorage.tokenExpire = writeData(ret.obj.tokenExpire);
									} else {
										delLocalStorage();
									}
								} else {
									delLocalStorage();
								}
							},
							error: function(err) {
								console.log(JSON.stringify(err))
							}
						})
					}
				}
			}

			function delLocalStorage() {
				localStorage.removeItem("account");
				localStorage.removeItem("phone");
				localStorage.removeItem("token");
				localStorage.removeItem("tokenExpire");
			}
		</script>
	</body>

</html>